package com.nidu.demo.mail.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.mail.convertor.MailTemplateConvertor;
import com.nidu.demo.mail.gateway.MailTemplateGateway;
import com.nidu.demo.mail.mapper.MailTemplateMapper;
import com.nidu.demo.mail.mapper.dataobject.MailTemplateDO;
import com.nidu.demo.mail.model.MailTemplate;
import com.nidu.demo.mail.model.MailTemplateCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 邮件模板网关实现
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailTemplateGatewayImpl implements MailTemplateGateway {

    private final MailTemplateMapper mapper;
    private final MailTemplateConvertor convertor;

    @Override
    public MailTemplate create(MailTemplate mailTemplate) {
        MailTemplateDO mailTemplateDO = convertor.toDataObject(mailTemplate);
        mapper.insert(mailTemplateDO);
        return convertor.toDomainObject(mailTemplateDO);
    }

    @Override
    public MailTemplate update(MailTemplate mailTemplate) {
        MailTemplateDO mailTemplateDO = convertor.toDataObject(mailTemplate);
        mapper.updateById(mailTemplateDO);
        return convertor.toDomainObject(mailTemplateDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public MailTemplate getById(Long id) {
        MailTemplateDO mailTemplateDO = mapper.selectById(id);
        if (mailTemplateDO == null) {
            return null;
        }
        return convertor.toDomainObject(mailTemplateDO);
    }

    @Override
    public List<MailTemplate> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<MailTemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(MailTemplateDO::getId, ids);
        List<MailTemplateDO> mailTemplateDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(mailTemplateDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(mailTemplateDOS);
    }

    @Override
    public MailTemplate getByCode(String code) {
        LambdaQueryWrapper<MailTemplateDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MailTemplateDO::getCode, code);
        wrapper.last("LIMIT 1");
        MailTemplateDO mailTemplateDO = mapper.selectOne(wrapper);
        if (mailTemplateDO == null) {
            return null;
        }
        return convertor.toDomainObject(mailTemplateDO);
    }

    @Override
    public List<MailTemplate> listByCondition(MailTemplateCriteria criteria) {
        LambdaQueryWrapper<MailTemplateDO> wrapper = buildQueryWrapper(criteria);
        List<MailTemplateDO> mailTemplateDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(mailTemplateDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(mailTemplateDOS);
    }

    @Override
    public PageResponse<MailTemplate> pageByCondition(MailTemplateCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<MailTemplateDO> wrapper = buildQueryWrapper(criteria);

        // 创建分页对象
        Page<MailTemplateDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }

        // 执行分页查询
        mapper.selectPage(page, wrapper);
        List<MailTemplate> mailTemplates = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(mailTemplates, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    public List<MailTemplate> listAll() {
        List<MailTemplateDO> mailTemplateDOS = mapper.selectList(null);
        if (CollUtil.isEmpty(mailTemplateDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(mailTemplateDOS);
    }

    private LambdaQueryWrapper<MailTemplateDO> buildQueryWrapper(MailTemplateCriteria criteria) {
        LambdaQueryWrapper<MailTemplateDO> wrapper = new LambdaQueryWrapper<>();
        
        // 状态条件
        wrapper.eq(criteria.getStatus() != null, MailTemplateDO::getStatus, criteria.getStatus());
        
        // 时间范围条件
        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(MailTemplateDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(MailTemplateDO::getCreateTime, createTime[0], createTime[1]);
            }
        }
        
        // 模糊查询条件
        wrapper.like(!StrUtil.isEmpty(criteria.getName()), MailTemplateDO::getName, criteria.getName());
        wrapper.like(!StrUtil.isEmpty(criteria.getCode()), MailTemplateDO::getCode, criteria.getCode());
        
        // 精确查询条件
        wrapper.eq(criteria.getAccountId() != null, MailTemplateDO::getAccountId, criteria.getAccountId());
        
        return wrapper;
    }

}
